package com.fun.wyunfei.timeline.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.fun.wyunfei.timeline.bean.Article;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据库测试
 * create by author vvyunfei on 2018/11/14 17
 */
public class DbService {

    private SQLiteDatabase database;

    public DbService(Context applicationContext) {
        SQLiteDbHelper helper = new SQLiteDbHelper(applicationContext);
        database = helper.getWritableDatabase();
    }

    public void insert(Article article) {
        ContentValues values = articleToContentValues(article);
        database.insert(SQLiteDbHelper.TABLE_ARTICLE, null, values);
    }

    public List<Article> queryArticle() {
        List<Article> articles = new ArrayList<>();
        // 相当于 select * from article 语句
        Cursor cursor = database.query(SQLiteDbHelper.TABLE_ARTICLE, null,
                null, null,
                null, null, "id DESC", "15");

        // 不断移动光标获取值
        while (cursor.moveToNext()) {
            // 直接通过索引获取字段值
//            int id = cursor.getInt(0);
            // 先获取 name 的索引值，然后再通过索引获取字段值
            String date = cursor.getString(cursor.getColumnIndex("date"));
            String title = cursor.getString(cursor.getColumnIndex("title"));
            String content = cursor.getString(cursor.getColumnIndex("content"));
            String other = cursor.getString(cursor.getColumnIndex("other"));
            Article article = new Article();
            article.setId(cursor.getInt(0));
            article.setDate(date);
            article.setTitle(title);
            article.setContent(content);
            article.setOther(other);
            articles.add(article);
        }
        // 关闭光标
        cursor.close();
        return articles;
    }

//    // delete all info
//    private void deleteAll() {
//        database.delete(SQLiteDbHelper.TABLE_ARTICLE,
//                null, null);
//    }


    public void deleteById(int id) {
        database.delete(SQLiteDbHelper.TABLE_ARTICLE,
                "id = ?", new String[]{id + ""});
    }

    private void updateArticle(Article article) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("content", article.getContent());
        contentValues.put("title", article.getTitle());
        contentValues.put("other", article.getOther());
        database.update(SQLiteDbHelper.TABLE_ARTICLE,
                contentValues, "id = ?", new String[]{"" + article.getId()});
    }

    // 将 article 对象的值存储到 ContentValues 中
    private ContentValues articleToContentValues(Article article) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("date", article.getDate());
        contentValues.put("title", article.getTitle());
        contentValues.put("content", article.getContent());
        contentValues.put("other", article.getOther());
        return contentValues;
    }

}
